package cn.sunlelin.educationserver.service.Impl;

import cn.sunlelin.educationserver.entity.Task;
import cn.sunlelin.educationserver.entity.Taskscore;
import cn.sunlelin.educationserver.mapper.TaskMapper;
import cn.sunlelin.educationserver.mapper.TaskscoreMapper;
import cn.sunlelin.educationserver.service.TaskService;
import cn.sunlelin.educationserver.service.TaskscoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private TaskscoreService taskscoreService;
    @Override
    public Boolean createTask(Task task) {
        task.setCreateTime(new Date());
        taskMapper.createTask(task);
        return true;
    }

    @Override
    public Boolean updateTask(Task task) {
        if(this.selectTaskById(task.getTaskId())==null)
            return false;
        task.setUpdateTime(new Date());
        taskMapper.updateTask(task);
        return true;
    }

    @Override
    public Task selectTaskById(Integer taskId) {
        return taskMapper.selectTaskById(taskId);
    }

    @Override
    public List<Map<String,Object>> selectTaskByCourseId(Integer courseId, Integer userId) {
        List<Map<String,Object>> taskList=taskMapper.selectTaskByCourseId(courseId);
        for (Map<String, Object> stringObjectMap : taskList) {
            System.out.println("taskId="+stringObjectMap.get("taskId"));
            if (taskscoreService.selectTaskscoreByUserAndTask(userId, (Integer) stringObjectMap.get("taskId")) != null) {
                stringObjectMap.put("subMitFlag", true);
            } else
                stringObjectMap.put("subMitFlag", false);
        }
        return taskList;
    }

    @Override
    public List<Task> selectTaskByChapterId(Integer chapterId) {
        return taskMapper.selectTaskByChapterId(chapterId);
    }

    @Override
    public Boolean deleteTask(Integer taskId) {
        if(this.selectTaskById(taskId)==null)
            return false;
        Task task=new Task();
        task.setTaskId(taskId);
        task.setUpdateTime(new Date());
        taskMapper.deleteTask(task);
        return true;
    }
}
